package cn.com.kblog.controller.admin;


import cn.com.kblog.common.Constant;
import cn.com.kblog.common.Result;
import cn.com.kblog.model.BlogType;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.activerecord.Page;

/**
 * 博客类型 Controller
 * @author KK.Luo
 * Email luokai8@vip.qq.com
 * @date 2013-11-30下午7:20:10
 */
@ControllerBind(controllerKey = "/admin/blogtype",viewPath = "/WEB-INF/pages/admin/blogtype")
public class BlogTypeController extends Controller{
	/**
	 * 分页查询BlogType
	 */
	public void list(){
		Integer pageNumber = getParaToInt(0,Constant.PAGE_NUM_DEFAULT);
		Integer pageSize = getParaToInt("pageSize", Constant.PAGE_SIZE_DEFAULT);
		String orderField = getPara("orderField", Constant.PAGE_ORDER_FIELD_DEFAULT);
		String orderDirection = getPara("orderDirection", Constant.PAGE_ORDER_DIRECTION_DEFAULT);
		Page<BlogType> beans = BlogType.dao.pageQuery(pageNumber, pageSize, orderField, orderDirection, null);
		setAttr(Constant.BEANS_KEY, beans);
		render("blogtype_list.html");
	}
	/**
	 * 编辑blogType
	 */
	public void edit(){
		String id = getPara(0);
		if (StringKit.isBlank(id)) {
		    render("blogtype_edit.html");
		    return;
		}
		BlogType bean = BlogType.dao.findById(id);
		if (bean == null) {
			render("blogtype_edit.html");
		    return;
		}
		setAttr(Constant.BEAN_KEY, bean);
		render("blogtype_edit.html");
	}
	/**
	 * 保存BlogType
	 */
	public void save(){
		BlogType bean = getModel(BlogType.class,Constant.BEAN_KEY);
		if(bean==null){
			keepPara();
			Result result =new Result(false, "参数错误!");
			setAttr(Constant.RESULT_KEY, result);
			render("blogtype_edit.html");
			return;
		}
		
		Result result = bean.saveOrUpdate();
		setAttr(Constant.RESULT_KEY, result);
		list();
	}
	
	
	/**
	 * 删除Blog
	 */
	public void delete(){
		String id = getPara(0);
		if (StringKit.isBlank(id)) {
			renderJson(new Result(false, "信息错误！"));
		    render("blogtype_list.html");
		    return;
		}
		Result result = BlogType.dao.deleteById(id);
		renderJson(result);
	}
}
